﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace HippoMapper.MapperService
{
    class HttpMapperContext : MapperContextBase
    {
        private static readonly string contextKey = "{641C47DC-C897-4b53-8B4C-B90CD154770A}";

        internal HttpMapperContext()
        {
            if (HttpContext.Current.Cache[contextKey] == null)
            {
                Stack<KeyValuePair<Type, IConditionParameter>> stack = new Stack<KeyValuePair<Type, IConditionParameter>>();

                HttpContext.Current.Cache[contextKey] = stack;

            }
        }

        public override void Push(Type type, IConditionParameter parameters)
        {
            Stack<KeyValuePair<Type, IConditionParameter>> stack = HttpContext.Current.Session[contextKey] as Stack<KeyValuePair<Type, IConditionParameter>>;

            stack.Push(new KeyValuePair<Type, IConditionParameter>(type, parameters));

            HttpContext.Current.Session[contextKey] = stack;
        }

        public override KeyValuePair<Type, IConditionParameter> Pop()
        {
            Stack<KeyValuePair<Type, IConditionParameter>> stack = HttpContext.Current.Session[contextKey] as Stack<KeyValuePair<Type, IConditionParameter>>;

            KeyValuePair<Type, IConditionParameter> record = stack.Pop();

            HttpContext.Current.Session[contextKey] = stack;

            return record;
        }

        public override bool Contains(KeyValuePair<Type, IConditionParameter> call)
        {
            Stack<KeyValuePair<Type, IConditionParameter>> stack = HttpContext.Current.Session[contextKey] as Stack<KeyValuePair<Type, IConditionParameter>>;

            return stack.Contains(call);
        }
    }
}
